package com.cems.controller.forum;

import com.cems.pojo.*;
import com.cems.service.EntrustService;
import com.cems.service.ForumService;
import com.cems.service.MessageService;
import com.cems.service.UserService;
import com.cems.util.LayuiReplay;
import com.cems.util.ShiroMd5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * @ClassName ForumController
 * @Author 陈新予(blank)
 * @Date 2021/5/18
 * @Version 1.0
 */
@Controller
public class ForumController {
    @Autowired
    ForumService forumService;
    @Autowired
    UserService userService;
    @Autowired
    EntrustService entrustService;
    @Autowired
    MessageService messageService;


    @GetMapping("toArticlePage")
    public String toArticlePage() {
        return "back/super/forme/textlist";
    }

    @GetMapping("toCommentPage")
    public String toCommentPage() {
        return "back/super/forme/commentlist";
    }

    @GetMapping("getAllArtcle")
    @ResponseBody
    public Object getArtcles(int page, int limit) {
        List<ForumArticle> articles = forumService.getArticle();
        List<ForumArticle> articleslimit = new ArrayList<>();
        for (int i = (page - 1) * limit; i < articles.size(); i++) {
            articleslimit.add(articles.get(i));
            if (articleslimit.size() == limit) {
                break;
            }
        }
        return new LayuiReplay<ForumArticle>(0, "OK", articles.size(), articleslimit);
    }

    // 获取委托信息
    @GetMapping("getAllEtrust")
    @ResponseBody
    public Object getEtrust(int page, int limit) {
        List<SysEntrust> entrusts = userService.getEtrust();
        List<SysEntrust> entrustslimit = new ArrayList<>();
        for (int i = (page - 1) * limit; i < entrusts.size(); i++) {
            entrustslimit.add(entrusts.get(i));
            if (entrustslimit.size() == limit) {
                break;
            }
        }
        return new LayuiReplay<SysEntrust>(0, "OK", entrusts.size(), entrustslimit);
    }

    // 获取所有委托的类型信息
    @GetMapping("getAllEtrustType")
    @ResponseBody
    public Object getAllEtrustType(int page, int limit) {
        List<SysEntrust> entrustsType = userService.getAllEtrustType();
        List<SysEntrust> entrustsTypelimit = new ArrayList<>();
        for (int i = (page - 1) * limit; i < entrustsType.size(); i++) {
            entrustsTypelimit.add(entrustsType.get(i));
            if (entrustsTypelimit.size() == limit) {
                break;
            }
        }
        return new LayuiReplay<SysEntrust>(0, "OK", entrustsType.size(), entrustsTypelimit);
    }

    @GetMapping("getUserMessage")
    @ResponseBody
    public Object getUserMessage(int page, int limit) {
        List<SysUserMessage> userMessages = userService.getUserMessages();
        List<SysUserMessage> userMeslimit = new ArrayList<>();
        for (int i = (page - 1) * limit; i < userMessages.size(); i++) {
            userMeslimit.add(userMessages.get(i));
            if (userMeslimit.size() == limit) {
                break;
            }
        }
        return new LayuiReplay<SysUserMessage>(0, "OK", userMessages.size(), userMeslimit);
    }

    @GetMapping("getAllComment")
    @ResponseBody
    public Object getComment(int page, int limit) {
        List<ForumComment> comment = forumService.getComment();
        List<ForumComment> commentslimit = new ArrayList<>();
        for (int i = (page - 1) * limit; i < comment.size(); i++) {
            commentslimit.add(comment.get(i));
            if (commentslimit.size() == limit) {
                break;
            }
        }
        return new LayuiReplay<ForumComment>(0, "OK", comment.size(), commentslimit);
    }

    @GetMapping("updateArtcle")
    public void updateArtcle(HttpSession session,int id, String status, HttpServletResponse resp) {
        try {
            PrintWriter out = resp.getWriter();
            if (status.equals("待审核"))
                forumService.updateArticle(id, "已审核");
            else
                forumService.updateArticle(id, "待审核");
            out.print("1");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            SysAdmin admin = (SysAdmin) session.getAttribute("loginAdmin");
            SysEcharts sys = userService.selOneSysSuc(admin.getId());
            Map<String,Object> map = new HashMap<>();
            map.put("adminId",admin.getId());
            map.put("operateNum",sys.getOperateNum()+1);
            userService.setSysLoginGraph(map);
        }

    }


    //管理员更改用户状态
    @GetMapping("upUserStatus")
    public void upUserStatus(HttpSession session, Integer userId, String status, HttpServletResponse resp) {
        try {
            PrintWriter out = resp.getWriter();
            if (status.equals("正常"))
                userService.upUserStatus(userId, "封禁");
            else
                userService.upUserStatus(userId, "正常");
            out.print("1");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            SysAdmin admin = (SysAdmin) session.getAttribute("loginAdmin");
            SysEcharts sys = userService.selOneSysSuc(admin.getId());
            Map<String,Object> map = new HashMap<>();
            map.put("adminId",admin.getId());
            map.put("operateNum",sys.getOperateNum()+1);
            userService.setSysLoginGraph(map);
        }

    }

    //管理员更改委托状态
    @GetMapping("upEtrustStatus")
    @ResponseBody
    public String upEtrustStatus(HttpSession session,Integer entrustId, String entState, HttpServletResponse resp) {
        try {
            //PrintWriter out = resp.getWriter();
            if (entState.equals("未审核")){
                userService.upEtrustStatus(entrustId, "已审核");
                SysEntrust leisureEntrustsByEntId = entrustService.getLeisureEntrustsByEntId(entrustId);
                Map<String,Object> map = new HashMap<>();
                map.put("userId",leisureEntrustsByEntId.getEntConsignor());
                map.put("userMessage","您的委托已经通过审核");
                messageService.addMessage(map);
                return "1";
                }
            else {
                userService.upEtrustStatus(entrustId, "未审核");
                SysEntrust leisureEntrustsByEntId = entrustService.getLeisureEntrustsByEntId(entrustId);
                Map<String, Object> map = new HashMap<>();
                map.put("userId", leisureEntrustsByEntId.getEntConsignor());
                map.put("userMessage", "您的委托未通过审核");
                messageService.addMessage(map);
                return "1";
            }
        } finally {
            SysAdmin admin = (SysAdmin) session.getAttribute("loginAdmin");
            SysEcharts sys = userService.selOneSysSuc(admin.getId());
            Map<String,Object> map = new HashMap<>();
            map.put("adminId",admin.getId());
            map.put("operateNum",sys.getOperateNum()+1);
            userService.setSysLoginGraph(map);
        }

    }

    @GetMapping("toReplypage")
    public String toReplyPage(int id, Model model) {
        model.addAttribute("CommentId", id);
        return "back/super/forme/replylist";
    }

    @GetMapping("getReplyByComment/{id}")
    @ResponseBody
    public Object getReplyByComment(@PathVariable("id") int id) {
        List<CommentReply> replyByComment = forumService.getReplyByComment(id);
        return new LayuiReplay<CommentReply>(0, "OK", replyByComment.size(), replyByComment);
    }

    @GetMapping("delReply")
    public void DelReply(HttpSession session, int id, HttpServletResponse resp) {
        PrintWriter out = null;
        try {
            out = resp.getWriter();
            forumService.delReply(id);
            out.print("1");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            SysAdmin admin = (SysAdmin) session.getAttribute("loginAdmin");
            SysEcharts sys = userService.selOneSysSuc(admin.getId());
            Map<String,Object> map = new HashMap<>();
            map.put("adminId",admin.getId());
            map.put("operateNum",sys.getOperateNum()+1);
            userService.setSysLoginGraph(map);
        }
    }

    //跳转到用户管理页面
    @GetMapping("toUserMgPage")
    public String toUserMgPage() {
        return "back/super/user/userList";
    }

    //跳转到委托管理页面
    @GetMapping("toEntrustPage")
    public String toEntrustPage() {
        return "back/super/user/etrustList";
    }


    //跳转到委托类型管理页面
    @GetMapping("toAddEntrustPage")
    public String toAddEntrustPage() {
        return "back/super/user/addEntrust";
    }

    @GetMapping("toEntrustByType/{typeID}")
    public Object toEntrustByType(@PathVariable("typeID") Integer typeID, Model model) {
        model.addAttribute("typeID", typeID);
        System.err.println("@PathVariable(\"typeID\") Integer typeID+======");
        return "back/super/user/byTypeEntrustlist";
    }

    @GetMapping("getEntrustByType/{typeID}")
    @ResponseBody
    public Object getEntrustByType(@PathVariable("typeID") Integer typeID) {
        System.err.println("@PathVariable(\"typeID\") Integer typeID+");
        List<SysEntrust> EntrustByTypes = userService.byEntrustByType(typeID);
        return new LayuiReplay<SysEntrust>(0, "OK", EntrustByTypes.size(), EntrustByTypes);
    }


    // 添加类型属性
    @PostMapping("addEntrustType")
    public String addEntrustType(Model model,
                                 String EntrustType,HttpSession session) {
        entrustService.addEntrustType(EntrustType);
        SysAdmin admin = (SysAdmin) session.getAttribute("loginAdmin");
        SysEcharts sys = userService.selOneSysSuc(admin.getId());
        Map<String,Object> map = new HashMap<>();
        map.put("adminId",admin.getId());
        map.put("operateNum",sys.getOperateNum()+1);
        userService.setSysLoginGraph(map);
        model.addAttribute("msg", "添加");
        return "back/super/user/addEntrust";
    }

    // 判断是否重复
    @GetMapping("judgeTypeRep")
    @ResponseBody
    public String judgeTypeRep(
            String EntrustType) {

        boolean bool = entrustService.judgeTypeRep(EntrustType);
        String msg = "";
        if (bool) {
            msg = "ok";
            System.err.println("ok");
        } else {
            msg = "失败--有重复属性";
            System.err.println("失败--有重复属性");
        }
        return msg;

    }


}
